const isArray = (obj: any): boolean => Object.prototype.toString.call(obj) === '[object Array]'

// Logger类的定义
const Logger = {
  // 以更漂亮的方式打印日志
  pretty(type: string = 'primary', title: string, text: any): void {
    if (typeof text === 'object') {
      console.group('Console Group', title)
      console.log(
        `%c ${title}`,
        `background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)};
          padding: 1px; border-radius: 4px; color: #fff;`
      )
      isArray(text) ? console.table(text) : console.dir(text)
      console.groupEnd()
      return
    }
    console.log(
      `%c ${title} %c ${text} %c`,
      `background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)};
        padding: 1px; border-radius: 4px 0 0 4px; color: #fff;`,
      `border:1px solid ${Logger.typeColor(type)};
        padding: 1px; border-radius: 0 4px 4px 0; color: ${Logger.typeColor(type)};`,
      'background:transparent'
    )
  },

  // 打印类型为 error 的漂亮日志
  prettyError(title: string, ...text: any[]): void {
    text.forEach((t) => Logger.pretty('error', title, t))
  },

  // 打印类型为 info 的漂亮日志
  prettyInfo(title: string, ...text: any[]): void {
    text.forEach((t) => Logger.pretty('info', title, t))
  },

  // 打印类型为 primary 的漂亮日志
  prettyPrimary(title: string, ...text: any[]): void {
    text.forEach((t) => Logger.pretty('primary', title, t))
  },

  // 打印类型为 success 的漂亮日志
  prettySuccess(title: string, ...text: any[]): void {
    text.forEach((t) => Logger.pretty('success', title, t))
  },

  // 打印类型为 warn 的漂亮日志
  prettyWarn(title: string, ...text: any[]): void {
    text.forEach((t) => Logger.pretty('warn', title, t))
  },

  // 打印日志
  print(type: string = 'default', text: any, back: boolean = false): void {
    if (typeof text === 'object') {
      // 如果是对象则调用打印对象方式
      isArray(text) ? console.table(text) : console.dir(text)
      return
    }
    if (back) {
      // 如果是打印带背景图的
      console.log(`%c ${text}`, `background:${Logger.typeColor(type)}; padding: 2px; border-radius: 4px; color: #fff;`)
    } else {
      console.log(
        `%c ${text}`,
        `border: 1px solid ${Logger.typeColor(type)};
          padding: 2px; border-radius: 4px;
          color: ${Logger.typeColor(type)};`
      )
    }
  },

  // 打印带背景的日志
  printBack(type: string = 'primary', text: any): void {
    Logger.print(type, text, true)
  },

  // 根据日志类型返回对应的颜色
  typeColor(type: string): string {
    let color = ''
    switch (type) {
      case 'error': {
        color = '#f03f14'
        break
      }
      case 'info': {
        color = '#909399'
        break
      }
      case 'primary': {
        color = '#2d8cf0'
        break
      }
      case 'success': {
        color = '#19be6b'
        break
      }
      case 'warn': {
        color = '#ff9900'
        break
      }
      default: {
        color = '#35495E'
        break
      }
    }
    return color
  }
}

export default Logger
